//
//  JPayMidView.m
//  HeadIndustry
//
//  Created by jun on 2017/9/5.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JPayMidView.h"
#import "JPayProductView.h"

@interface JPayMidView()

@property (nonatomic, strong) JShopCarShopModel *shopModel;
@property (nonatomic, strong) YYLabel *storeName;
@property (nonatomic, strong) YYLabel *promiseLa;
@property (nonatomic, strong) NSMutableArray *cellViewArr;
@property (nonatomic, assign) CGFloat lastMaxY;

@end

@implementation JPayMidView


- (instancetype)initWithShopModel:(JShopCarShopModel *)shopModel;
{
    if (self = [super init]) {
        _shopModel = shopModel;
        _cellViewArr = [NSMutableArray array];
        self.backgroundColor = [UIColor whiteColor];
        self.opaque = NO;
        [self setUpView];
    }
    return  self;
}

- (void)setUpView {
    YYLabel *storeNamela = [YYLabel new];
    storeNamela.frame = CGRectMake(15, 0, kWindowW - 30, 40);
    storeNamela.font = kFONT_TITLE(18);
    [self addSubview:storeNamela];
    storeNamela.text = _shopModel.VendorName;
    _storeName = storeNamela;
    
    for (int i = 0; i < _shopModel.goodsArray.count; i++) {
        JPayProductView *cellView = [[JPayProductView alloc]initWithFrame:CGRectMake(0, storeNamela.bottom + i * (100 + 10), kWindowW, 100)];
        [cellView reloadDataWithModel:_shopModel.goodsArray[i]];       
        [self addSubview:cellView];
        [_cellViewArr addObject:cellView];

    }
    
    YYLabel *promiseLa = [YYLabel new];
    promiseLa.frame = CGRectMake(15, 40 + _shopModel.CarItems.count * 110, kWindowW - 30, 40);
    promiseLa.font = kFONT_TITLE(16);
    [self addSubview:promiseLa];
    promiseLa.text = @"此商品支持7天退换";
    _promiseLa = promiseLa;
    
    CALayer *line1 = [CALayer layer];
    line1.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line1.frame = CGRectMake(15, promiseLa.bottom - 0.5, kWindowW - 15, 0.5);
    [self.layer addSublayer:line1];
    
    // 配送
    YYLabel *distributionLa = [YYLabel new];
    distributionLa.frame = CGRectMake(15, promiseLa.bottom, 75, 40);
    distributionLa.font = kFONT_TITLE(16);
    [self addSubview:distributionLa];
    distributionLa.text = @"配送方式";
    
    CALayer *rightImg = [CALayer layer];
    rightImg.contents = (__bridge id _Nullable)([UIImage imageNamed:rightBtnImg].CGImage);
    rightImg.frame = CGRectMake(kWindowW - 15 - 10, distributionLa.top + 9 , 10, 22);
    [self.layer addSublayer:rightImg];
    
    // 配送   快递  包邮
    YYLabel *modeLa = [YYLabel new];
    modeLa.frame = CGRectMake(kWindowW - 30 - 90, promiseLa.bottom, 90, 40);
    modeLa.font = kFONT_TITLE(16);
    [self addSubview:modeLa];
    modeLa.textAlignment = NSTextAlignmentRight;
    modeLa.text = @"快递  包邮";
    
    CALayer *line2 = [CALayer layer];
    line2.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line2.frame = CGRectMake(15, distributionLa.bottom - 0.5, kWindowW - 15, 0.5);
    [self.layer addSublayer:line2];
    
    // 买家留言
    YYLabel *leaveLa = [YYLabel new];
    leaveLa.frame = CGRectMake(15, distributionLa.bottom, 75, 40);
    leaveLa.font = kFONT_TITLE(16);
    [self addSubview:leaveLa];
    leaveLa.text = @"买家留言:";
    
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(leaveLa.right +5, leaveLa.top, kWindowW - leaveLa.right - 5, 40)];
    textField.placeholder = @"留下您的需求（选填）";
    textField.font = kFONT_TITLE(16);
    [textField addTarget:self action:@selector(textFieldValueChangeMethod:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:textField];
    _customerMessage = textField;
    
    CALayer *line3 = [CALayer layer];
    line3.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line3.frame = CGRectMake(15, leaveLa.bottom - 0.5, kWindowW - 15, 0.5);
    [self.layer addSublayer:line3];
    
    // 小计
    YYLabel *SumPriceLa = [YYLabel new];
    SumPriceLa.frame = CGRectMake(kWindowW - 15 - 50, leaveLa.bottom, 80, 40);
    SumPriceLa.font = kFONT_TITLE(16);
    SumPriceLa.textColor = [UIColor redColor];
    [self addSubview:SumPriceLa];
    SumPriceLa.text = _shopModel.TotalMoneyText;
    
    // 小计
    YYLabel *SumPriceLaText = [YYLabel new];
    SumPriceLaText.frame = CGRectMake(SumPriceLa.left - 40, leaveLa.bottom, 40, 40);
    SumPriceLaText.font = kFONT_TITLE(16);
    [self addSubview:SumPriceLaText];
    SumPriceLaText.text = @"小计:";
    
    
    YYLabel *productNumLa = [YYLabel new];
    productNumLa.frame = CGRectMake(15, leaveLa.bottom, SumPriceLa.left - 60 - 15 , 40);
    productNumLa.font = kFONT_TITLE(16);
    productNumLa.textAlignment = NSTextAlignmentRight;
    [self addSubview:productNumLa];
    productNumLa.text = [NSString stringWithFormat:@"共%ld件商品",(long)_shopModel.Qty];
    
}

- (void)textFieldValueChangeMethod:(UITextField *)textField
{
    if (_textFieldBlock) {
        _textFieldBlock(textField.text);
    }
}

- (CGFloat)getHeight
{
    CGFloat height = _shopModel.goodsArray.count * 110 + (40 * 5);
    return  height;
}

@end
